﻿using EnvDTE;

namespace Skim.Ssms.AddIn.ActiveFileExplorer.Loader
{
    public static class DocumentInfoFactory
    {
        public static IDocumentInfo GetInfo(Document document)
        {
            if (DocumentTypeFilter.IsQueryDocument(document))
                return new QueryDocumentInfo(document);

            if (DocumentTypeFilter.IsSolutionDocument(document))
                return new SolutionDocumentInfo(document);

            if (DocumentTypeFilter.IsDesignerDocument(document))
                return new DesignerDocumentInfo(document);

            return new NullDocumentInfo(document);
        }
    }

    public interface IDocumentInfo
    {
        Document Document { get; }
        TreeViewImageIndex DocumentImageIndex { get; }
        string GetDocumentName();
    }

    internal abstract class DocumentInfo : IDocumentInfo
    {
        public Document Document { get; private set; }
        public abstract TreeViewImageIndex DocumentImageIndex { get; }

        protected DocumentInfo(Document document)
        {
            Document = document;
        }

        public abstract string GetDocumentName();
    }

    internal class QueryDocumentInfo : DocumentInfo
    {
        public QueryDocumentInfo(Document document) 
            : base(document)
        {
        }

        public override TreeViewImageIndex DocumentImageIndex
        {
            get { return TreeViewImageIndex.QueryDocument; }
        }

        public override string GetDocumentName()
        {
            return Document.ProjectItem.Name;
        }
    }

    internal class SolutionDocumentInfo : DocumentInfo
    {
        public SolutionDocumentInfo(Document document) 
            : base(document)
        {
        }

        public override TreeViewImageIndex DocumentImageIndex
        {
            get { return TreeViewImageIndex.SolutionDocument; }
        }

        public override string GetDocumentName()
        {
            return Document.Name;
        }
    }

    internal class DesignerDocumentInfo : DocumentInfo
    {
        public DesignerDocumentInfo(Document document) 
            : base(document)
        {
        }

        public override TreeViewImageIndex DocumentImageIndex
        {
            get { return TreeViewImageIndex.DesignDocument; }
        }

        public override string GetDocumentName()
        {
            return Document.ActiveWindow.Caption;
        }
    }

    internal class NullDocumentInfo : QueryDocumentInfo
    {
        public NullDocumentInfo(Document document) 
            : base(document)
        {
        }
    }
}